package com.web.controoller;

import com.alibaba.druid.support.json.JSONUtils;
import com.web.dao.TestDao;
import com.web.pojo.dto.testDto;
import com.web.pojo.vo.CommonResult;
import com.web.service.AddressServer;
import com.web.service.NetworkServer;
import com.web.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author: wws
 * @Date: 2021/1/4 23:27
 * describe:
 */

@RestController
public class Test {

    @Autowired
    TestDao testDao;

    @Autowired
    NetworkServer networkServer;




    @PostMapping("/test")
    @ApiOperation("test")
    @ResponseBody
    public CommonResult test(@RequestBody testDto t) {
        System.out.println(t);
        return CommonResult.success("ok");
    }

    @GetMapping("/test1")
    @ApiOperation("test1")
    @ResponseBody
    public String test1() {
        byte[] bytes = testDao.get();
        System.out.println(Arrays.toString(bytes));
        return "ok";
    }

    @GetMapping("/login")
    @ApiOperation("login")
    @ResponseBody
    public String login() {
        System.out.println("login+++++++++++");
        return "hello";
    }

    @GetMapping("/hello")
    @ApiOperation("hello")
    public CommonResult hello() {
        CommonResult commonResult = CommonResult.fail("haha");
        return commonResult;
    }

    @PreAuthorize("hasAuthority('test1')")
    @GetMapping("/hello1")
    @ApiOperation("hello1")
    public CommonResult hello1() {
        System.out.println("==============");
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getName());
        System.out.println("==============");
        System.out.println(SecurityUtils.getUsername());
        System.out.println("==============");
        return CommonResult.success("haha11111");
    }

    @PreAuthorize("hasAuthority('test2')")
    @GetMapping("/hello2")
    @ApiOperation("hello2")
    public CommonResult hello2() {
        Integer username = SecurityUtils.getUserId();
        return CommonResult.success("hhhh");
    }

    @PreAuthorize("hasAuthority('test3')")
    @GetMapping("/hello3")
    @ApiOperation("hello3")
    public CommonResult hello3() {
        return CommonResult.success("haha2222");
    }


    public void out(HttpServletResponse response) {
        try {
            String test = JSONUtils.toJSONString("asd");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = null;
            out = response.getWriter();
            out.append(test);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @ResponseBody
//    @RequestMapping("/async01")
//    public Callable<String> async01() {
//        System.out.println("主线程开始..." + Thread.currentThread() + "==>" + System.currentTimeMillis());
//        Callable<String> callable = new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                byte[] take = TestHandler.inQueue.take();
//                System.out.println(Arrays.toString(take));
//                return "blake";
//            }
//        };
//        System.out.println("主线程结束..." + Thread.currentThread() + "==>" + System.currentTimeMillis());
//        return callable;
//    }

//    @ResponseBody
//    @RequestMapping("/test")
//    public Callable<String> test() {
//        System.out.println("addGateWay");
//        byte[] testBHead = {0x01, 0x01, 0x00, 0x00, 0x01, 0x01,};
//        byte[] devEui = {0x00, 0x02, (byte) 0xe4, (byte) 0x95, 0x6e, 0x4b, 0x0b, (byte) 0xbe};
//        byte[] testBytes = new byte[testBHead.length + devEui.length];
//        System.arraycopy(testBHead, 0, testBytes, 0, testBHead.length);
//        System.arraycopy(devEui, 0, testBytes, testBHead.length, devEui.length);
//        System.out.println(Arrays.toString(testBytes));
//
//        Callable<String> callable = new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                byte[] take = TestHandler.inQueue.take();
//                byte[] a = Arrays.copyOfRange(take, 1, 3);
//                System.out.println(Arrays.toString(take));
//                return Arrays.toString(a);
//            }
//        };
//        System.out.println("主线程结束..." + Thread.currentThread() + "==>" + System.currentTimeMillis());
//        return callable;
//    }

//    @ResponseBody
//    @RequestMapping("/addGateWay")
//    public String addGateWay() {
//        System.out.println("addGateWay");
//        byte[] testBHead = {0x01, 0x01, 0x00, 0x00, 0x01, 0x01,};
//        byte[] devEui = {0x00, 0x02, (byte) 0xe4, (byte) 0x95, 0x6e, 0x4b, 0x0b, (byte) 0xbe};
//        byte[] testBytes = new byte[testBHead.length + devEui.length];
//        System.arraycopy(testBHead, 0, testBytes, 0, testBHead.length);
//        System.arraycopy(devEui, 0, testBytes, testBHead.length, devEui.length);
//        System.out.println("发送" + Arrays.toString(testBytes));
////        WebServer.map.get("a").writeAndFlush(Unpooled.copiedBuffer(testBytes));
//        return "发送!";
//    }
}
